/*
 * maim.cpp
 *
 *  Created on: 06.10.2014
 *      Author: Ralph
 */

#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QDateTime>

#include <buildtime.h>

#include "gui/mainwindow.h"

QTextStream *out = 0;
bool logToFile = false;
bool verbose = false;
bool debug = false;
bool makeScreenshot = false;

void logOutput( QtMsgType type, const QMessageLogContext& context, const QString &message )
{
    QString filedate = QDateTime::currentDateTime().toString( "yyyy.MM.dd hh:mm:ss:zzz" );
    QString debugdate = QDateTime::currentDateTime().toString( "hh:mm:ss:zzz" );

    switch ( type )
    {
    case QtDebugMsg:
        debugdate += " [D]";
        if ( debug )
        {
            (*out) << debugdate << " " << message << endl;
        }
        break;
    case QtWarningMsg:
        debugdate += " [W]";
        (*out) << debugdate << " " << message << endl;
        break;
    case QtCriticalMsg:
        debugdate += " [C]";
        (*out) << debugdate << " " << message << endl;
        break;
    case QtFatalMsg:
        debugdate += " [F]";
        (*out) << debugdate << " " << message << endl;
        break;
    }

    if ( logToFile )
    {
        QFile outFile("log.txt");
        outFile.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream ts(&outFile);
        ts << filedate << " " << message << endl;
    }

}

void noOutput( QtMsgType type, const QMessageLogContext& context, const QString &message )
{
}

int main( int argc, char *argv[] )
{
#ifdef __DEBUG__
    debug = true;
    verbose = true;
#endif

    QString hg = HGTIP;
    hg.remove( ";" );
    hg.remove( "changeset:" );
    hg.replace( " ","" );

    qDebug() << "paperboy development version" << hg << BUILDDATE << BUILDTIME;
    qDebug() << "(c) 2014 Ralph Schurade";
    qDebug() << "Submit suggestions, feature requests, bug reports to https://code.google.com/p/paperboy/";

    QApplication app( argc, argv );

    QCoreApplication::setOrganizationDomain( "braingl.de" );
	QCoreApplication::setOrganizationName( "BrainGL" );
	QCoreApplication::setApplicationName( "paperboy" );
	QCoreApplication::setApplicationVersion( "0.0.1" );

    Q_INIT_RESOURCE( resources );

    QStringList args = app.arguments();

    bool resetSettings = false;

    QList<QString> filesToLoad;

    for ( int i = 1; i < args.size(); ++i )
    {
        QString arg = args.at( i );
        if ( arg.size() == 2 && arg.startsWith( '-' ) )
        {
            char c = arg[ arg.length() - 1 ].toLatin1();

            switch( c )
            {
                case 'd':
                    debug = true;
                    break;
                case 'h':
                    qDebug() << "Command line options:";
                    qDebug() << "-d : debug mode, show more messages at the console";
                    qDebug() << "-h : displays this message";
                    qDebug() << "-l : logs debug messages to text file";
                    qDebug() << "-r : resets saved settings";
                    exit( 0 );
                    break;
                case 'l':
                    logToFile = true;
                    break;
                case'r':
                    // reset saved settings
                    resetSettings = true;
                    break;
            }
        }
    }

    qInstallMessageHandler( noOutput );
    out = new QTextStream( stdout );
    qInstallMessageHandler( logOutput );

    MainWindow mainWin( debug, resetSettings );
    mainWin.show();

    return app.exec();
}
